package com.nbcio.pm.service.impl;

import com.nbcio.pm.domain.vo.*;
import com.nbcio.pm.mapper.PmReportStatsMapper;
import com.nbcio.pm.service.IPmReportStatsService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

/**
 * 项目管理报表统计Service业务层处理
 *
 * @author nbacheng
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class PmReportStatsServiceImpl implements IPmReportStatsService {

    private final PmReportStatsMapper reportStatsMapper;

    @Override
    public PmProjectStatsVo getProjectStats(String tenantId) {
        return reportStatsMapper.selectProjectStats(tenantId);
    }

    @Override
    public PmTaskStatsVo getTaskStats(String tenantId, Long projectId) {
        return reportStatsMapper.selectTaskStats(tenantId, projectId);
    }

    @Override
    public PmTimeStatsVo getTimeStats(String tenantId, Long projectId, LocalDate startDate, LocalDate endDate) {
        return reportStatsMapper.selectTimeStats(tenantId, projectId, startDate, endDate);
    }

    @Override
    public PmMemberStatsVo getMemberStats(String tenantId, Long projectId) {
        return reportStatsMapper.selectMemberStats(tenantId, projectId);
    }

    @Override
    public List<PmProjectProgressStatsVo> getProjectProgressStats(String tenantId) {
        return reportStatsMapper.selectProjectProgressStats(tenantId);
    }

    @Override
    public List<PmTaskStatusStatsVo> getTaskStatusStats(String tenantId, Long projectId) {
        return reportStatsMapper.selectTaskStatusStats(tenantId, projectId);
    }

    @Override
    public List<PmTimeTrackingStatsVo> getTimeTrackingStats(String tenantId, Long projectId, LocalDate startDate, LocalDate endDate) {
        return reportStatsMapper.selectTimeTrackingStats(tenantId, projectId, startDate, endDate);
    }

    @Override
    public PmReportStatsVo getComprehensiveStats(String tenantId, Long projectId, LocalDate startDate, LocalDate endDate) {
        PmReportStatsVo stats = new PmReportStatsVo();
        
        // 获取各项统计 - 现在需要手动构建内部类对象
        PmReportStatsVo.ProjectStats projectStats = new PmReportStatsVo.ProjectStats();
        PmProjectStatsVo projectData = getProjectStats(tenantId);
        if (projectData != null) {
            projectStats.setTotalProjects(projectData.getTotalProjects());
            projectStats.setActiveProjects(projectData.getActiveProjects());
            projectStats.setCompletedProjects(projectData.getCompletedProjects());
            projectStats.setOverdueProjects(projectData.getOverdueProjects());
            projectStats.setCompletionRate(projectData.getCompletionRate());
        }
        stats.setProjectStats(projectStats);
        
        PmReportStatsVo.TaskStats taskStats = new PmReportStatsVo.TaskStats();
        PmTaskStatsVo taskData = getTaskStats(tenantId, projectId);
        if (taskData != null) {
            taskStats.setTotalTasks(taskData.getTotalTasks());
            taskStats.setCompletedTasks(taskData.getCompletedTasks());
            taskStats.setInProgressTasks(taskData.getInProgressTasks());
            taskStats.setPendingTasks(taskData.getPendingTasks());
            taskStats.setOverdueTasks(taskData.getOverdueTasks());
            taskStats.setTaskCompletionRate(taskData.getTaskCompletionRate());
        }
        stats.setTaskStats(taskStats);
        
        PmReportStatsVo.TimeStats timeStats = new PmReportStatsVo.TimeStats();
        PmTimeStatsVo timeData = getTimeStats(tenantId, projectId, startDate, endDate);
        if (timeData != null) {
            timeStats.setTotalHours(timeData.getTotalHours());
            timeStats.setBillableHours(timeData.getBillableHours());
            timeStats.setNonBillableHours(timeData.getNonBillableHours());
            timeStats.setAverageHoursPerDay(timeData.getAverageHours());
            timeStats.setTotalRecords(timeData.getTotalRecords());
        }
        stats.setTimeStats(timeStats);
        
        PmReportStatsVo.MemberStats memberStats = new PmReportStatsVo.MemberStats();
        PmMemberStatsVo memberData = getMemberStats(tenantId, projectId);
        if (memberData != null) {
            memberStats.setTotalMembers(memberData.getTotalMembers());
            memberStats.setActiveMembers(memberData.getActiveMembers());
            memberStats.setInactiveMembers(memberData.getInactiveMembers());
            memberStats.setNewMembersThisMonth(memberData.getNewMembersThisMonth());
        }
        stats.setMemberStats(memberStats);
        
        // 转换列表数据
        List<PmReportStatsVo.ProjectProgressStats> projectProgressList = new ArrayList<>();
        List<PmProjectProgressStatsVo> projectProgressData = getProjectProgressStats(tenantId);
        if (projectProgressData != null) {
            for (PmProjectProgressStatsVo item : projectProgressData) {
                PmReportStatsVo.ProjectProgressStats progressStats = new PmReportStatsVo.ProjectProgressStats();
                progressStats.setProjectId(item.getProjectId());
                progressStats.setProjectName(item.getProjectName());
                progressStats.setProgress(item.getProgress());
                progressStats.setStatus(item.getStatus());
                progressStats.setStartDate(item.getStartDate());
                progressStats.setEndDate(item.getEndDate());
                progressStats.setTotalTasks(item.getTotalTasks());
                progressStats.setCompletedTasks(item.getCompletedTasks());
                projectProgressList.add(progressStats);
            }
        }
        stats.setProjectProgressStats(projectProgressList);
        
        List<PmReportStatsVo.TaskStatusStats> taskStatusList = new ArrayList<>();
        List<PmTaskStatusStatsVo> taskStatusData = getTaskStatusStats(tenantId, projectId);
        if (taskStatusData != null) {
            for (PmTaskStatusStatsVo item : taskStatusData) {
                PmReportStatsVo.TaskStatusStats statusStats = new PmReportStatsVo.TaskStatusStats();
                statusStats.setStatus(item.getStatus());
                statusStats.setStatusName(item.getStatusName());
                statusStats.setCount(item.getCount());
                statusStats.setPercentage(item.getPercentage());
                taskStatusList.add(statusStats);
            }
        }
        stats.setTaskStatusStats(taskStatusList);
        
        List<PmReportStatsVo.TimeTrackingStats> timeTrackingList = new ArrayList<>();
        List<PmTimeTrackingStatsVo> timeTrackingData = getTimeTrackingStats(tenantId, projectId, startDate, endDate);
        if (timeTrackingData != null) {
            for (PmTimeTrackingStatsVo item : timeTrackingData) {
                PmReportStatsVo.TimeTrackingStats trackingStats = new PmReportStatsVo.TimeTrackingStats();
                trackingStats.setDate(item.getDate());
                trackingStats.setHours(item.getHours());
                trackingStats.setRecordCount(item.getRecordCount());
                trackingStats.setProjectName(item.getProjectName());
                trackingStats.setTaskName(item.getTaskName());
                trackingStats.setUserName(item.getUserName());
                timeTrackingList.add(trackingStats);
            }
        }
        stats.setTimeTrackingStats(timeTrackingList);
        
        return stats;
    }
}
